package com.sys.SysSales.service.impl;

import java.util.List;


import com.sys.common.utils.DateUtils;
import com.sys.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sys.SysSales.mapper.SysSalesSummaryMapper;
import com.sys.SysSales.domain.SysSalesSummary;
import com.sys.SysSales.service.ISysSalesSummaryService;

/**
 * 销售统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-28
 */
@Service
public class SysSalesSummaryServiceImpl implements ISysSalesSummaryService
{
    @Autowired
    private SysSalesSummaryMapper sysSalesSummaryMapper;

    /**
     * 查询销售统计
     * 
     * @param summaryId 销售统计主键
     * @return 销售统计
     */
    @Override
    public SysSalesSummary selectSysSalesSummaryBySummaryId(Long summaryId)
    {
        return sysSalesSummaryMapper.selectSysSalesSummaryBySummaryId(summaryId);
    }

    /**
     * 查询销售统计列表
     * 
     * @param sysSalesSummary 销售统计
     * @return 销售统计
     */
    @Override
    public List<SysSalesSummary> selectSysSalesSummaryList(SysSalesSummary sysSalesSummary)
    {
        return sysSalesSummaryMapper.selectSysSalesSummaryList(sysSalesSummary);
    }

    /**
     * 新增销售统计
     * 
     * @param sysSalesSummary 销售统计
     * @return 结果
     */
    @Override
    public int insertSysSalesSummary(SysSalesSummary sysSalesSummary)
    {
        sysSalesSummary.setCreateTime(DateUtils.getNowDate());
        sysSalesSummary.setUpdatedAt(DateUtils.getNowDate());
        sysSalesSummary.setCreatedBy(SecurityUtils.getUsername());
        sysSalesSummary.setUpdatedBy(SecurityUtils.getUsername());
        return sysSalesSummaryMapper.insertSysSalesSummary(sysSalesSummary);
    }

    /**
     * 修改销售统计
     * 
     * @param sysSalesSummary 销售统计
     * @return 结果
     */
    @Override
    public int updateSysSalesSummary(SysSalesSummary sysSalesSummary)
    {
        return sysSalesSummaryMapper.updateSysSalesSummary(sysSalesSummary);
    }

    /**
     * 批量删除销售统计
     * 
     * @param summaryIds 需要删除的销售统计主键
     * @return 结果
     */
    @Override
    public int deleteSysSalesSummaryBySummaryIds(Long[] summaryIds)
    {
        return sysSalesSummaryMapper.deleteSysSalesSummaryBySummaryIds(summaryIds);
    }

    /**
     * 删除销售统计信息
     * 
     * @param summaryId 销售统计主键
     * @return 结果
     */
    @Override
    public int deleteSysSalesSummaryBySummaryId(Long summaryId)
    {
        return sysSalesSummaryMapper.deleteSysSalesSummaryBySummaryId(summaryId);
    }



    /**
     * 按时间查询金额等信息
     */
    @Override
    public SysSalesSummary getSalesStatByTime(String beginTime, String endTime, Boolean dayFlag,Boolean monthFlag, Boolean yearFlag) {
        // 1. 参数校验
        if ((beginTime != null && endTime == null) || (beginTime == null && endTime != null)) {
            throw new IllegalArgumentException("自定义时间段查询需同时传递开始时间和结束时间");
        }

        // 2. 调用Mapper查询销售统计数据
        SysSalesSummary salesSummary = sysSalesSummaryMapper.selectOrdersByCreateTime(
                beginTime, endTime, dayFlag, monthFlag, yearFlag
        );
        SysSalesSummary summary = sysSalesSummaryMapper.selectTopSellingProductByTime(
                dayFlag, monthFlag, yearFlag, beginTime, endTime
        );
        SysSalesSummary summary1 = sysSalesSummaryMapper.getSalesVolumeByTimeRange(
                dayFlag, monthFlag, yearFlag, beginTime, endTime
        );
        if (summary1!=null){
            salesSummary.setTotalQuantity(summary1.getTotalQuantity());
        }
        if (summary==null){
            salesSummary.setTopProductName("未销售");
        }
        if (summary!=null){
        salesSummary.setTopProductName(summary.getTopProductName());

        }
        // 3. 结果处理
        return salesSummary;
    }

//



//    @Override
//    public String getSalesVolumeByTimeRange(Boolean dayFlag, Boolean monthFlag, Boolean yearFlag, String startTime, String endTime) {
//        SysSalesSummary summary = sysSalesSummaryMapper.getSalesVolumeByTimeRange(
//                dayFlag, monthFlag, yearFlag, startTime, endTime
//        );
//
//        // 业务逻辑处理（空值判断等）
//        return summary != null ? summary.getTotalQuantity(): "无销售记录";
//    }


//    @Override
//    public List<SysSalesSummary> selectSysSalesSummaryLista(SysSalesSummary sysSalesSummary) {
//        return sysSalesSummaryMapper.selectSysSalesSummaryLista(sysSalesSummary);
//    }
}
